package cn.cytong.nk0713.services;

import cn.cytong.nk0713.utils.nutz.CustomisedNutDao;
import cn.cytong.rpc.server.NutzRpcService;
import flexjson.JSONSerializer;
import org.nutz.dao.entity.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * product产片信息
 */
public class ProductService extends NutzRpcService{
    private Logger logger = LoggerFactory.getLogger(ProductService.class);
    private CustomisedNutDao nutDao;

    /**
     * 通过景区获取产品信息
     * @param params    vId     景区Id
     * @return          productViewpoints[productViewpoint(viewpoint,product)]  产品及景区信息
     *                  success     true or false
     */
    public String getProductsByViewpoint(Map params) {
        Map result = new HashMap();
        Object vId = params.get("vId");
        try {
            List<Record> productViewpoints =nutDao.queryTableRecord("ProductViewpoint", "viewpoint_id = ?", vId);
            for (Record productViewpoint : productViewpoints) {
                Record viewpoint = nutDao.fetchTableRecord("viewpoint", "id = ?", productViewpoint.get("viewpoint_id"));
                Record product = nutDao.fetchTableRecord("product", "id = ?", productViewpoint.get("product_id"));
                productViewpoint.put("viewpoint", viewpoint);
                productViewpoint.put("product", product);
            }
            result.put("productViewpoints", productViewpoints);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("error in method ProductService.getProductsByViewpoint", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return new JSONSerializer().include("productViewpoints").serialize(result);
    }
}
